<?php

namespace App\Http\Controllers\Platform\Config;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Platform\Config\Betting\SaveRequest;
use App\Http\Resources\Common\DatasResource;
use App\Http\Resources\Common\DataResource;
use App\Services\Platform\Config\BettingService;
use phpDocumentor\Reflection\Types\Integer;

class BettingController extends Controller
{

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total) = BettingService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total);
    }

    /**
     * 保存数据
     *
     * @param int $id
     */
    public function save(SaveRequest $request)
    {
        BettingService::make()->save($request);

        return new DataResource(null);
    }

    /**
     * 删除数据
     *
     * @param int $id
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        BettingService::make()->destroy($id);

        return new DataResource([$id]);
    }

    /**
     * 获取币种列表
     */
    public function getCurrencyList(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64'
        ]);
        $platCode = $request->input('plat_code');

        $currencies = BettingService::make()->getCurrencyList($platCode);

        return new DataResource(
            $currencies->sortBy('currency')->values()->all()
        );
    }

    /**
     * 使用默认投注配置覆盖平台投注配置
     */
    public function fullDefaultBets(Request $request)
    {
        $data = $this->validate($request, [
            'plat_code' => 'required|string|max:64',
            'game_id' => 'required|string|max:64',
            'bet_type' => 'required|Integer',
        ]);

        $config = BettingService::make()->fullDefaultBets(
            $data['plat_code'], $data['game_id'], $data['bet_type']
        );

        return new DataResource($config);
    }

    /**
     * 使用目标源投注配置覆盖平台投注配置
     */
    public function fullSourceBets(Request $request)
    {
        $data = $request->validate([
            'sourcePlatCode' => 'required|string|max:64',
            'targetPlatCodes' => 'required|array|min:1',
            'games' => 'nullable|array',
        ]);

        BettingService::make()->fullSourceBets(
            $data['sourcePlatCode'],
            $data['targetPlatCodes'],
            $data['games']
        );

        return new DataResource($data);
    }
}
